package com.cat.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cat.yygh.hosp.repository.DepartnentRepository;
import com.cat.yygh.hosp.service.DepartmentService;
import com.cat.yygh.model.hosp.Department;
import com.cat.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    /*注入持久化层操作Mongodb*/
    private DepartnentRepository departnentRepository;
    @Override
    public void saveDepartment(Map<String, Object> stringObjectMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(stringObjectMap), Department.class);
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department platformDepartment = departnentRepository.findByHoscodeAndDepcode(hoscode,depcode);
        if (platformDepartment == null){
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departnentRepository.save(department);
        }else {
            department.setCreateTime(platformDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(platformDepartment.getIsDeleted());

            department.setId(platformDepartment.getId());
            departnentRepository.save(department);
        }
    }

    @Override
    public Page<Department> getDepartmentPage(Map<String, Object> stringObjectMap) {
        /*强转数据类型*/
        Integer limit = Integer.parseInt((String)stringObjectMap.get("limit"));
        Integer page = Integer.parseInt((String)stringObjectMap.get("page"));
        String hoscode = (String)stringObjectMap.get("hoscode");
        /*将查询条件转换成Example*/
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> departmentExample = Example.of(department);
        /*Spring data构造分页请求对象*/
        PageRequest pageRequest = PageRequest.of(page, limit);
        return departnentRepository.findAll(departmentExample, pageRequest);
    }

    @Override
    public void removeDepartment(Map<String, Object> stringObjectMap) {
        String hoscode = (String)stringObjectMap.get("hoscode");
        String depcode = (String)stringObjectMap.get("depcode");
        Department platformDepartment = departnentRepository.findByHoscodeAndDepcode(hoscode,depcode);
        if (platformDepartment != null){
            departnentRepository.deleteById(platformDepartment.getId());
        }
    }

    @Override
    public List<DepartmentVo> getDepartmentList(String hospcode) {
        //根据医院编号，查询医院所有科室信息
        Department department = new Department();
        department.setHoscode(hospcode);
        Example<Department> example = Example.of(department);
        //所有科室列表 all
        List<Department> all = departnentRepository.findAll(example);
        //创建一个返回的数组对象
        List<DepartmentVo> bigDepartmentVoList = new ArrayList<DepartmentVo>();
        //stream()方法将列表对象 all 转换为一个流 对部门列表进行分组按 bigcode=>[]这样子格式数据
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //collect.entrySet() 将{} 转换成数组数据
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            //获取科室代码
            String bigCode = entry.getKey();
            //获取大科室分组对应的数据
            List<Department> value = entry.getValue();
            //新建一个大科室对象
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            //设置大科室对象的的科室代码
            bigDepartmentVo.setDepcode(bigCode);
            //设置大科室对象的的科室名称第一个子对象
            bigDepartmentVo.setDepname(value.get(0).getBigname());
            /**
             * 封装子元素数据
             */
            //创建一个子科室对象数组
            List<DepartmentVo> childDepartmentList = new ArrayList<>();
            for (Department childDepartment : value) {
                //创建一个子科室对象
                DepartmentVo departmentVo = new DepartmentVo();
                String depCode = childDepartment.getDepcode();
                String depName = childDepartment.getDepname();
                departmentVo.setDepcode(depCode);
                departmentVo.setDepname(depName);
                //添加到子科室数组数据
                childDepartmentList.add(departmentVo);
            }
            //把小科室list集合放到大科室children里面
            bigDepartmentVo.setChildren(childDepartmentList);
            /**封装结束*/
            //往数组里面添加对象数据
            bigDepartmentVoList.add(bigDepartmentVo);
        }
        return bigDepartmentVoList;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departnentRepository.getDepartnentByHoscodeAndDepcode(hoscode,depcode);
        if (department != null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departnentRepository.getDepartnentByHoscodeAndDepcode(hoscode,depcode);
    }
}
